package com;



import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class GamePanel extends JPanel implements Observer {
    private Shape shape; //图形
    private String scores;
    private GameContainer gameContainer; //游戏进行容器
    private List<GameContainer.Block[]> bolcks;
    TextField textField;

    public GamePanel() {
        setBackground(new Color(0x7B7D74));
        setSize(GameContainer.COLS*shape.SIZE*2,GameContainer.ROWS*Shape.SIZE*2);
        setLayout(null);
        gameContainer=new GameContainer();
        gameContainer.addObserver(this);//添加监视者-动作监听
        gameContainer.start();
        Panel menuPanel=new Panel();
        menuPanel.setSize(GameContainer.COLS*Shape.SIZE,GameContainer.ROWS*Shape.SIZE);
        menuPanel.setBackground(Color.white);
        add(menuPanel);
        menuPanel.setBounds(gameContainer.COLS*Shape.SIZE,0,gameContainer.COLS*Shape.SIZE,GameContainer.ROWS*Shape.SIZE);
        JLabel label = new JLabel("您的得分：");
        textField = new TextField();
        textField.setText("0");
        menuPanel.add(label);
        menuPanel.setLayout(null);
        textField.setSize(100,30);
        textField.setBounds(50,70,100,50);
        menuPanel.add(textField);
        label.setSize(100,30);
        label.setBounds(50,40,100,20);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) { //键位动作
                switch (e.getKeyCode()){
                    case KeyEvent.VK_UP://上键变形
                        if(gameContainer.canDown()&&gameContainer.canLeft()&&gameContainer.canRight()){
                            shape.rotate();
                            repaint();
                        }
                        break;

                    case KeyEvent.VK_DOWN://下键预加速
                        if(gameContainer.canDown()){
                            shape.down();
                            repaint();
                        }break;

                        case KeyEvent.VK_LEFT:
                            if(gameContainer.canLeft()){
                                shape.moveLeft();
                                repaint();
                            }break;

                    case KeyEvent.VK_RIGHT:
                        if(gameContainer.canRight()){
                            shape.moveRight();
                            repaint();
                        }break;
                }
            }
        });

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(shape!=null)
            drawShape(g);
        if(bolcks!=null)
            drawBlocks(g);
    }

    private void drawBlocks(Graphics g) {
        for(int i=0;i<bolcks.size();i++){
            GameContainer.Block[] block=bolcks.get(i);
            for(int j=0;j<block.length;j++){
                if(block[j].value==1){
                    g.setColor(block[j].color);
                    g.fill3DRect(j*Shape.SIZE,i*Shape.SIZE,Shape.SIZE,Shape.SIZE,true);
                }
            }
        }
    }

    private void drawShape(Graphics g) {
        g.setColor(shape.getColor());
        int left=shape.getLeft();
        int top=shape.getTop();
        int[][] data=shape.getData();
        for(int i=0;i<data.length;i++)
            for(int j=0;j<data[i].length;j++){
                if(data[i][j]==1)
                g.fill3DRect((left+j)*shape.SIZE,(top+i)*shape.SIZE,shape.SIZE,shape.SIZE,true);
            }
    }

    @Override
    public void update(Observable o, Object arg) {
        if(arg instanceof Shape){
            shape=(Shape)arg;
            repaint();
        }
        else if(arg instanceof List){
            bolcks=(List<GameContainer.Block[]>)arg;
        }

        if(gameContainer.getScoresStr()!=null){
            textField.setText(gameContainer.getScoresStr());
        }
    }
}
